package controllers

import (
	"fmt"
	"mime/multipart"
	"path"
	"social-app/config"
	"social-app/pkg/encryp"
	"strconv"
	"strings"
	"time"
)

func GetUploadFilePath(filename string, fileType string) string {
	saveImageName := path.Join(config.AppConfig.UploadPath, fileType)
	saveImageName = path.Join(saveImageName, strconv.Itoa(time.Now().Year()))
	saveImageName = path.Join(saveImageName, strconv.Itoa(int(time.Now().Month())))
	saveImageName = path.Join(saveImageName, strconv.Itoa(time.Now().Day()))
	parts := strings.Split(filename, ".")
	newFilename := encryp.Md5(fmt.Sprintf("%s_%s", parts[0], time.Now()))
	newFilename = fmt.Sprintf("%s%s", newFilename, path.Ext(filename))
	saveImageName = path.Join(saveImageName, newFilename)
	return saveImageName
}

func CheckUploadImageExt(file *multipart.FileHeader, exts map[string]string) bool {
	ext := file.Header.Get("Content-Type")
	fmt.Println("ext", ext)
	allow := false
	for _, v := range exts {
		if v == ext {
			allow = true
			break
		}
	}
	return allow
}
